<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\LoginCheck;

class LoginController extends Controller
{
	/**
	 * 显示登录页面
	 */
    public function Login()
    {
    	return View('admin.login');
    }

    /**
     * 处理登录
     */
   	public function doLogin(LoginCheck $request)
   	{
   		if (!$request->action) {
   			return redirect('admin/Login');
   		}

   		$res = User::where('name', $request->name)->first();
   		if (!$res) {
   			return redirect('admin/Login')->with(['warning' => '用户不存在']);
   		}
   		$pass = User::select('password')->where('name', $request->name)
   										->first();
   		$bool = password_verify($request->password, $pass->password);

   		if (!$bool) {
   			return redirect('admin/Login')->with(['warning' => '密码错误']);
   		}
      session(['user' => $res]);
   		return redirect('admin/CommodityType')->with(['warning' => '欢迎您，'.$request->name]);
   	}
    /**
     * 处理退出
     */
    public function LogOut()
    {
      session()->forget('user');
      return redirect('admin/Login');
    }

}
